package com.ccl.iot;import java.util.ArrayList;
import java.util.HashMap;

public class IOTCallBackHolder{
	public static class NoIDCallBackHolder<TYPE>{
	  	private ArrayList<TYPE> iCallBackList = new ArrayList<TYPE>();
	  	
	  	public boolean addCallBack(TYPE aCallBack){
	  		if (aCallBack != null){
	  			if (iCallBackList.contains(aCallBack)){
	  				return false;
	  			}
	  			
	  			return iCallBackList.add(aCallBack);
	  		}
	  		
	  		return false;
	  	}
	  	
	   	public boolean removeCallBack(TYPE aCallBack){
	   		if (aCallBack != null){
	   			return iCallBackList.remove(aCallBack);
	   		}
	   		
	   		return false;
	   	}
	   	
	   	public ArrayList<TYPE> getCallBackList(){
	   		return iCallBackList;
	   	}
	}
	
	public static class IDCallBackHolder<TYPE>{
		private HashMap<Object, ArrayList<TYPE>> iCallBack = null;
		
		public boolean addCallBack(TYPE aCallBack, Object aID){
			if (aCallBack != null && aID != null){
				ArrayList<TYPE> tList = null;
				
				if (iCallBack == null){
					iCallBack = new HashMap<Object, ArrayList<TYPE>>();
				}else if (iCallBack.containsKey(aID)){
					tList = iCallBack.get(aID);
					
					if (tList.contains(aCallBack)){
						return false;
					}
				}
				
				if (tList == null){
					tList = new ArrayList<TYPE>();
					
					if (tList.add(aCallBack)){
						iCallBack.put(aID, tList);
						
						return true;
					}
				}else{
					return tList.add(aCallBack);
				}
			}
			
			return false;
		}
		
		public boolean removeCallBack(TYPE aCallBack, Object aID){
			if (iCallBack != null && aCallBack != null && aID != null){
				if (iCallBack.containsKey(aID)){
					ArrayList<TYPE> tList = iCallBack.get(aID);
					
					if (tList.remove(aCallBack)){
						if (tList.size() == 0){
							iCallBack.remove(aID);
						}
						
						return true;
					}
				}
			}
			
			return false;
		}
		
		public ArrayList<TYPE> getCallBackList(Object aID){
			if (iCallBack != null){
				return iCallBack.get(aID);
			}
			
			return null;
		}
	}
}